package file;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.TitledBorder;
import javax.swing.border.EmptyBorder;

public class FileTransferPanel extends JPanel implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	FileTransfer fu = null;
	FileClientThread fileClientThread = null;
	
	protected final String FILESERVERIP = "127.0.0.1";
	private JPanel panel;
	private JButton btnNewButton;
	private JPanel panel_1;
	private JPanel panel_2;
	private JProgressBar progressBar;
	private JPanel panel_4;
	JLabel label_file;
	JLabel label_fileName;

	public String fileName ;
	public String filePath;
	long fileSize;
	/**
	 * Create the panel.
	 */
	public FileTransferPanel() {
		setLayout(new BorderLayout(0, 0));
		add(getPanel_1(), BorderLayout.CENTER);
	}
	
	public void setFup(FileTransfer fileTransfer) {
		this.fu = fileTransfer;
	}
	
	public void setAddId(String filePath, String name) {
		this.filePath = filePath;
		this.fileName = name;
	}
	
	public void setProgressbar(int current){
		
		progressBar.setValue(current);
	}
	
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(new Color(255, 255, 255));
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnNewButton(), BorderLayout.WEST);
		}
		return panel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("  삭  제  ");
			btnNewButton.setBackground(new Color(173, 216, 230));
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String isKey = FileTransferPanel.this.getToolTipText();
					
					fu.setB(10);
					
					Iterator<FileTransferPanel> iter = fu.vectorFileTransferPanels.iterator();
					int count = 0;
					while(iter.hasNext()){
						FileTransferPanel setMap = iter.next();
						if(setMap.getToolTipText().equals(isKey)){
							fu.vectorFileTransferPanels.remove(count);
							System.out.println("파일 정보를 담은 Vector의 file_info에 저장된 filename: "+fu.vector_fileInfo.get(count).getFileName()+"가 목록에서 삭제 되었습니다.");
							System.out.println("파일 정보를 담은 Vector의 file_info의 목록 삭제 전 사이즈: "+fu.vector_fileInfo.size());
							fu.vector_fileInfo.remove(count);
							System.out.println("파일 정보를 담은 Vector의 file_info의 목록 삭제 후 사이즈: "+fu.vector_fileInfo.size());
							FileTransferPanel.this.setVisible(false);
							fu.setNumber(fu.getNumber()-1);
							break;
						}
						count++;
					}
					
					Iterator<FileTransferPanel> iter2 = fu.vectorFileTransferPanels.iterator();
					while(iter2.hasNext()){
						FileTransferPanel setMap =  iter2.next();
						setMap.setBounds(10, fu.getB(), 380, 45);
						fu.setB(fu.getB()+45);
						fu.panel_5.updateUI();
					}
					if(fu.getB() == 10){
						fu.setOrder(0);
					}
					fu.labelNameRedefine();
				}
				
			});
			btnNewButton.setFont(new Font("맑은 고딕", Font.BOLD, 12));
			btnNewButton.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			btnNewButton.setBorderPainted(false);
		}
		return btnNewButton;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new BorderLayout(0, 0));
			panel_1.add(getPanel(), BorderLayout.EAST);
			panel_1.add(getPanel_2(), BorderLayout.NORTH);
			panel_1.add(getPanel_4(), BorderLayout.CENTER);
			panel_1.add(getLabel_file(), BorderLayout.WEST);
		}
		return panel_1;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setBackground(new Color(255, 255, 255));
		}
		return panel_2;
	}
	private JProgressBar getProgressBar() {
		if (progressBar == null) {
			progressBar = new JProgressBar();
			progressBar.setBackground(new Color(255, 255, 255));
			progressBar.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
			progressBar.setStringPainted(true);
			progressBar.setOpaque(true);
		}
		return progressBar;
	}
	private JPanel getPanel_4() {
		if (panel_4 == null) {
			panel_4 = new JPanel();
			panel_4.setBorder(new EmptyBorder(0, 5, 0, 5));
			panel_4.setBackground(new Color(255, 255, 255));
			panel_4.setLayout(new BorderLayout(0, 0));
			panel_4.add(getProgressBar(), BorderLayout.NORTH);
			panel_4.add(getLabel_filename(), BorderLayout.SOUTH);
		}
		return panel_4;
	}
	private JLabel getLabel_file() {
		if (label_file == null) {
			label_file = new JLabel("  파일 1  ");
			label_file.setBackground(new Color(255, 255, 255));
			label_file.setFont(new Font("맑은 고딕", Font.BOLD, 12));
			label_file.setOpaque(true);
		}
		return label_file;
	}
	private JLabel getLabel_filename() {
		if (label_fileName == null) {
			label_fileName = new JLabel("");
			label_fileName.setBorder(new EmptyBorder(1, 0, 0, 0));
			label_fileName.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		}
		return label_fileName;
	}
}
